package com.cirkana.viewer.game;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.cirkana.CirkanaActivity;
import com.cirkana.CirkanaUtil;
import com.cirkana.viewer.R;

public class MemoryGameActivity extends CirkanaActivity {

	private ImageView[][] imageViews = new ImageView[4][4];
	private Button backButton;
	private Bitmap[][] images = new Bitmap[4][4];
	private Bitmap backImage;
	private boolean[][] matched = { { false, false, false, false },
			{ false, false, false, false }, { false, false, false, false },
			{ false, false, false, false } };
	private int[] current = new int[2];
	private int[] current2 = new int[2];

	private int state = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memory);

		this.imageViews[0][0] = (ImageView) findViewById(R.id.imageView11);
		this.imageViews[0][1] = (ImageView) findViewById(R.id.imageView12);
		this.imageViews[0][2] = (ImageView) findViewById(R.id.imageView13);
		this.imageViews[0][3] = (ImageView) findViewById(R.id.imageView14);
		this.imageViews[1][0] = (ImageView) findViewById(R.id.imageView21);
		this.imageViews[1][1] = (ImageView) findViewById(R.id.imageView22);
		this.imageViews[1][2] = (ImageView) findViewById(R.id.imageView23);
		this.imageViews[1][3] = (ImageView) findViewById(R.id.imageView24);
		this.imageViews[2][0] = (ImageView) findViewById(R.id.imageView31);
		this.imageViews[2][1] = (ImageView) findViewById(R.id.imageView32);
		this.imageViews[2][2] = (ImageView) findViewById(R.id.imageView33);
		this.imageViews[2][3] = (ImageView) findViewById(R.id.imageView34);
		this.imageViews[3][0] = (ImageView) findViewById(R.id.imageView41);
		this.imageViews[3][1] = (ImageView) findViewById(R.id.imageView42);
		this.imageViews[3][2] = (ImageView) findViewById(R.id.imageView43);
		this.imageViews[3][3] = (ImageView) findViewById(R.id.imageView44);

		String[] imageUrls = getResources().getStringArray(
				R.array.memorygameimages);

		try {
			this.images[0][0] = CirkanaUtil.getImageBitmap(imageUrls[1]);
			this.images[0][1] = CirkanaUtil.getImageBitmap(imageUrls[5]);
			this.images[0][2] = CirkanaUtil.getImageBitmap(imageUrls[3]);
			this.images[0][3] = CirkanaUtil.getImageBitmap(imageUrls[2]);
			this.images[1][0] = CirkanaUtil.getImageBitmap(imageUrls[7]);
			this.images[1][1] = this.images[0][2];
			this.images[1][2] = this.images[1][0];
			this.images[1][3] = CirkanaUtil.getImageBitmap(imageUrls[0]);
			this.images[2][0] = this.images[0][3];
			this.images[2][1] = CirkanaUtil.getImageBitmap(imageUrls[6]);
			this.images[2][2] = this.images[1][3];
			this.images[2][3] = this.images[0][0];
			this.images[3][0] = this.images[0][1];
			this.images[3][1] = CirkanaUtil.getImageBitmap(imageUrls[4]);
			this.images[3][2] = this.images[2][1];
			this.images[3][3] = this.images[3][1];
			this.backImage = CirkanaUtil.getImageBitmap(imageUrls[8]);
		} catch (Exception e) {
			showErrorMessage(e);
		}
		
		for (int iii = 0; iii < 4; iii++) 
			for (int jjj = 0; jjj < 4; jjj++) 
				this.imageViews[iii][jjj].setImageBitmap(this.backImage);


		this.backButton = (Button) findViewById(R.id.memory_back);

		for (int iii = 0; iii < 4; iii++) {
			for (int jjj = 0; jjj < 4; jjj++) {
				this.imageViews[iii][jjj]
						.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View imageView) {
								int[] r = getIndexsOfImage((ImageView) imageView);
								if (!matched[r[0]][r[1]]
										&& !(r[0] == current[0] && r[1] == current[1])
										&& state != 2 && state != 3) {
									((ImageView) imageView)
											.setImageBitmap(images[r[0]][r[1]]);
									if (state == 0) {
										current = r;
										state = 1;
									} else if (state == 1) {
										if (images[r[0]][r[1]] == images[current[0]][current[1]]) {
											matched[r[0]][r[1]] = true;
											matched[current[0]][current[1]] = true;
											if (isFinished()) {
												backButton.setEnabled(true);
												backButton.setText("Done!");
												state = 3;
											} else
												state = 0;
										} else {
											current2 = r;
											state = 2;
											backButton.setEnabled(true);
										}
									}
								}
							}
						});
			}
		}

		this.backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (state == 2) {
					try {
						imageViews[current[0]][current[1]]
								.setImageBitmap(backImage);
					} catch (Exception e) {
						showErrorMessage(e);
					}
					try {
						imageViews[current2[0]][current2[1]]
								.setImageBitmap(backImage);
					} catch (Exception e) {
						showErrorMessage(e);
					}
					state = 0;
					backButton.setEnabled(false);
				} else if (state == 3) {
					finish();
				}
			}
		});
	}

	private int[] getIndexsOfImage(ImageView imageView) {
		int ri = -1;
		int rj = -1;
		for (int iii = 0; iii < 4; iii++) {
			for (int jjj = 0; jjj < 4; jjj++) {
				if (imageView == imageViews[iii][jjj]) {
					ri = iii;
					rj = jjj;
				}
			}
		}

		int[] r = { ri, rj };
		return r;
	}

	private boolean isFinished() {
		for (int iii = 0; iii < 4; iii++)
			for (int jjj = 0; jjj < 4; jjj++)
				if (!matched[iii][jjj])
					return false;
		return true;
	}

	public static Intent createIntent(Context context) {
		Intent intent = new Intent(context, MemoryGameActivity.class);
		return intent;
	}
}
